package com.example.artemis.config;

/**
 * 应用程序配置类
 * 集中管理所有硬编码的配置参数
 */
public class ApplicationConfig {

    // ================== 生产者和消费者配置 ==================
    public static final int NUMBER_OF_PRODUCERS = 3;
    public static final int NUMBER_OF_CONSUMERS = 5;
    public static final int MESSAGES_PER_PRODUCER = 20;
    public static final double FAILURE_RATE = 0.1;  // 10% 失败率用于演示

    // ================== 演示运行配置 ==================
    public static final long DEMO_DURATION_SECONDS = 30;  // 演示持续时间（秒）
    public static final long STATISTICS_DISPLAY_INTERVAL_SECONDS = 5;  // 统计信息显示间隔（秒）
    public static final long CONSUMER_STARTUP_WAIT_TIME = 2000;  // 消费者启动等待时间（毫秒）
    public static final long MESSAGE_PROCESSING_WAIT_TIME = 3000;  // 消息处理等待时间（毫秒）

    // ================== 日志配置 ==================
    public static final String LOG_LEVEL = "INFO";
    public static final boolean ENABLE_DEBUG_LOGGING = false;

    // ================== 资源管理配置 ==================
    public static final long THREAD_POOL_TERMINATION_TIMEOUT_SECONDS = 60;  // 线程池终止超时时间
    public static final long RESOURCE_CLEANUP_TIMEOUT_SECONDS = 5;  // 资源清理超时时间

    // ================== 统计服务配置 ==================
    public static final int STATISTICS_CLEANUP_INTERVAL_MINUTES = 5;  // 统计清理间隔（分钟）
    public static final int MAX_TIME_WINDOWS = 100;  // 最大时间窗口数量
    public static final int TIME_WINDOW_SIZE_MINUTES = 5;  // 时间窗口大小（分钟）

    // ================== 系统配置 ==================
    public static final String APP_NAME = "ActiveMQ Artemis Demo Application";
    public static final String APP_VERSION = "2.0.0";

    // ================== 配置获取方法 ==================

    /**
     * 获取生产者配置信息
     */
    public static String getProducerConfigInfo() {
        return String.format("生产者配置: 数量=%d, 每个消息数=%d, 失败率=%.1f%%",
                           NUMBER_OF_PRODUCERS, MESSAGES_PER_PRODUCER, FAILURE_RATE * 100);
    }

    /**
     * 获取消费者配置信息
     */
    public static String getConsumerConfigInfo() {
        return String.format("消费者配置: 数量=%d", NUMBER_OF_CONSUMERS);
    }

    /**
     * 获取演示配置信息
     */
    public static String getDemoConfigInfo() {
        return String.format("演示配置: 持续时间=%d秒, 统计显示间隔=%d秒",
                           DEMO_DURATION_SECONDS, STATISTICS_DISPLAY_INTERVAL_SECONDS);
    }

    /**
     * 获取所有配置信息
     */
    public static String getAllConfigInfo() {
        StringBuilder configInfo = new StringBuilder();
        configInfo.append("=== 应用程序配置信息 ===\n");
        configInfo.append(String.format("应用名称: %s\n", APP_NAME));
        configInfo.append(String.format("应用版本: %s\n", APP_VERSION));
        configInfo.append(getProducerConfigInfo()).append("\n");
        configInfo.append(getConsumerConfigInfo()).append("\n");
        configInfo.append(getDemoConfigInfo()).append("\n");
        configInfo.append(String.format("队列分片数: %d\n", EmbeddedServerConfig.NUMBER_OF_QUEUES));
        configInfo.append(String.format("消息持久化: %s\n", "启用"));
        configInfo.append(String.format("传递语义: %s\n", "至少一次"));
        configInfo.append("======================");
        return configInfo.toString();
    }

    /**
     * 验证配置参数的合理性
     */
    public static boolean validateConfig() {
        boolean isValid = true;
        StringBuilder errors = new StringBuilder();

        if (NUMBER_OF_PRODUCERS <= 0) {
            errors.append("生产者数量必须大于0\n");
            isValid = false;
        }

        if (NUMBER_OF_CONSUMERS <= 0) {
            errors.append("消费者数量必须大于0\n");
            isValid = false;
        }

        if (MESSAGES_PER_PRODUCER <= 0) {
            errors.append("每个生产者的消息数必须大于0\n");
            isValid = false;
        }

        if (FAILURE_RATE < 0 || FAILURE_RATE > 1) {
            errors.append("失败率必须在0到1之间\n");
            isValid = false;
        }

        if (DEMO_DURATION_SECONDS <= 0) {
            errors.append("演示持续时间必须大于0\n");
            isValid = false;
        }

        if (!isValid) {
            throw new IllegalArgumentException("配置验证失败:\n" + errors.toString());
        }

        return isValid;
    }
}